package com.zzt.zt_edittextinput.util;

import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: zeting
 * @date: 2022/3/22
 * 输入自带空格操作   << >>
 */
public class NumberCardSpaceFilter implements InputFilter {
    public static final String TAG = NumberCardSpaceFilter.class.getSimpleName();

    public static final char[] CHARACTERS = new char[]{
//            ' ',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
            'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    };


    private char mSplitChar = ' ';// 默认间隔符
    private int mSplitPosition = 4; // 间隔距离
    private int mMaxLength;// 最终长度

    public NumberCardSpaceFilter(int mSplitPosition, int mMax) {
        this.mSplitPosition = mSplitPosition;
        // 商
        int quotient = mMax / mSplitPosition;
        // 余数
        int remainder = mMax % mSplitPosition;
        if (remainder == 0) {
            mMaxLength = mMax + quotient - 1;
        } else {
            mMaxLength = mMax + quotient;
        }
        Log.w(TAG, "间隔:" + mSplitPosition + " 输入：" + mMax + " 商：" + quotient + " 余数：" + remainder + " 输入最大长度：" + mMaxLength);
    }

    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        Log.w(TAG, "source:" + source + " start:" + start + " end:" + end + " dest:" + dest + " dstart:" + dstart + " dend:" + dend);

        /**
         * 控制字符长度
         */
        if (!okEditLength(source, dest)) {
            return "";
        }

        /**
         * 控制是否想要输入字符
         */
        if (!okEditChar(source, start, end)) {
            return "";
        }

//        // 判断空格位置是否正确
//        if (end - start == 1) {
//            if (source.charAt(0) == mSplitChar) {
//                if (dstart % mSplitPosition != 0) {
//                    return "";
//                }
//            }
//        }

        /**
         * 校验输入规则是否合规
         */
//        if (end - start == 1) {
//            // 一个个输入时候校验输入规则
//            if (!okEditInput(source, dest)) {
//                Log.d(TAG, "输入规则异常");
//                return "";
//            }
//        } // 批量时候有可能是粘贴就不校验了

        /**
         * 添加时候，格式校验
         */
        if (end - start > 0) {
            // 多余的字符串
            int destLength = dest.length();
            // 不能被整除部分
            int destML = dest.length() % (mSplitPosition + 1);
            Log.d(TAG, "整除剩余 ： " + destML + "   被截取的剩余部分：" + dest.subSequence(destLength - destML, destLength));
            if (source.length() + destML <= mSplitPosition) {
                Log.d(TAG, "在正常范围内" + (source.length() + destML));
                return null;
            } else {
                Log.d(TAG, "跨间隔符了" + (source.length() + destML));
                // 这里处理跨间隔符
                StringBuilder aa = new StringBuilder();
                int length = source.length();
                for (int i = 0; i < length; i++) {
                    if (i == 0 && destLength == 0) {
                        aa.append(source.charAt(i));
                    } else {
                        int d2 = destML + i;
                        if (d2 % mSplitPosition == 0) {
                            char c = source.charAt(i);
                            aa.append(mSplitChar).append(source.charAt(i));
                        } else {
                            aa.append(source.charAt(i));
                        }
                    }
                }
                Log.d(TAG, "最后添加的数据：" + aa);
                return aa;
            }
        }
        return null;
    }


    /**
     * 判断是否规定格式
     */
    public boolean okEditInput(CharSequence source, Spanned dest) {
        String repStr = String.valueOf(dest) + source;
        String regex = "([\\w]{4}\\s)*[\\w]{0,4}";
        Pattern pattern = Pattern.compile(regex);
        Matcher match = pattern.matcher(repStr);
        return match.matches();
    }


    /**
     * 获取字符真实长度
     */
    public boolean okEditLength(CharSequence source, Spanned dest) {
        String repStr = String.valueOf(dest) + source;
        int length = repStr.length();
        if (length > mMaxLength) {
            return false;
        }
        return true;
    }

    /**
     * 判断输入字符是否符合规则
     */
    public boolean okEditChar(CharSequence source, int start, int end) {
        char[] accept = getAcceptedChars();
        for (int i = start; i < end; i++) {
            if (!ok(accept, source.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    private char[] getAcceptedChars() {
        return CHARACTERS;
    }

    private boolean ok(char[] accept, char c) {
        for (int i = accept.length - 1; i >= 0; i--) {
            if (accept[i] == c) {
                return true;
            }
        }

        return false;
    }
}